/* **************************************************************
 * 
 * @auth zmx@gootop.net
 * 百度地图 for mark point
 * 
 * **************************************************************/
//命名空间
gtm = {};

//参数空间
gtm.p = {
	geocoder:null,
	map:null,
	point:null,
	coord:null,
	lastInfoWindow:false,
	lastMarker:false,
	companyname :'',// 公司名称
	companyaddress :''// 公司地址
};

//方法空间
gtm.f = {};

//初使化地图
gtm.f.initialize = function(){
	gtm.p.geocoder = new BMap.Geocoder();
	
	gtm.p.map = new BMap.Map("map_canvas");            // 创建Map实例
	gtm.p.point = new BMap.Point(108.969446,34.282437);    // 创建点坐标
	gtm.p.map.centerAndZoom(gtm.p.point,15);                     // 初始化地图,设置中心点坐标和地图级别。
	gtm.p.map.enableScrollWheelZoom(); 
	
	gtm.p.map.addEventListener("click", function(e){   
		 var point = new BMap.Point(e.point.lng, e.point.lat); 
		 var marker = new BMap.Marker(point,{
			 enableDragging: true
         });
		 var contentHtml = '';
    	 contentHtml+= 'lng:'+e.point.lng+'<br/>';
    	 contentHtml+= 'lat:'+e.point.lat+'<br/>';
    	 marker.addEventListener('dragend',function(e){
    		 var zoom= gtm.f.getZoom();
	       	  var add_coord = "{'lat':'"+e.point.lat+"','lng':'"+e.point.lng+"','zoom':"+zoom+"}";
         	  var contentHtml = 'lat:'+e.point.lat+'<br/>lng:';
         	  	  contentHtml+= e.point.lng+'<br/>';
	       	  gtm.p.lastInfoWindow.setContent(contentHtml);
	       	  gtm.p.coord = add_coord; 
    	 });
    	 zoom= gtm.f.getZoom();
    	 var add_coord = "{'lat':'"+e.point.lat+"','lng':'"+e.point.lng+"','zoom':"+zoom+"}";
    	 gtm.p.coord = add_coord; 
    	 gtm.p.map.addOverlay(marker);
    	 gtm.f.createInfoWindow(marker,contentHtml);
	});    
	
};

//创建InfoWindow
gtm.f.createInfoWindow = function(Marker,message){
	var infowindow = new BMap.InfoWindow(message,
      { 
	    maxWidth:0
      });
	
	 gtm.f.openInfoWindow(infowindow,Marker);
	  //绑定事件
	 Marker.addEventListener("click", function(){    
		 gtm.f.openInfoWindow(infowindow,Marker);
	 }); 
};

//打开InfoWindow
gtm.f.openInfoWindow = function(infoWindow,Marker){
	if(gtm.p.lastInfoWindow == infoWindow)return;
	gtm.f.removeLastShow();
	gtm.p.lastInfoWindow = infoWindow;
	gtm.p.lastMarker = Marker;
	Marker.openInfoWindow(infoWindow);
};

//移除上次标记
gtm.f.removeLastShow = function(){
	if(gtm.p.lastMarker){
		gtm.p.lastMarker.hide();
		if(gtm.p.lastInfoWindow){
			gtm.p.lastMarker.closeInfoWindow();
		}
	}
};
gtm.f.geocoding = function(address){
	if (gtm.p.geocoder) {
		gtm.p.geocoder.getPoint(address, function(results) {
	        if (results) {
	          gtm.p.map.setCenter(results);
	          var point = new BMap.Point(results.lng, results.lat); 
	 		  var marker = new BMap.Marker(point,{
	 			 enableDragging: true
	          });
	 		  
	          var contentHtml = 'lat:';
	          	  contentHtml+= results.lat+'<br/>lng:';
	          	  contentHtml+= results.lng+'<br/>';
	          	  
	          gtm.p.map.addOverlay(marker);
	          gtm.f.createInfoWindow(marker,contentHtml);

	          marker.addEventListener('dragend', function(e) {
	        	  var zoom= gtm.f.getZoom();
	        	  var add_coord = "{'lat':'"+e.point.lat+"','lng':'"+e.point.lng+"','zoom':"+zoom+"}";
	          	  var contentHtml = 'lat:'+e.point.lat+'<br/>lng:';
	          	  	  contentHtml+= e.point.lng+'<br/>';
	        	  gtm.p.lastInfoWindow.setContent(contentHtml);
	        	  gtm.p.coord = add_coord; 
	          });
	          
              var zoom= gtm.f.getZoom();
              var add_coord = "{'lat':'"+results.lat+"','lng':'"+results.lng+"','zoom':"+zoom+"}";
              gtm.p.coord = add_coord; 
	        } else {
	          alert("Geocode was not successful for the following reason: " + status);
	        }
	      });
	    }
};

//添加标记
gtm.f.setMark = function(jsonObject){
	
	 var point = new BMap.Point(jsonObject.lng, jsonObject.lat); 
	 var marker = new BMap.Marker(point,{
		 enableDragging: true
     });
	  
	gtm.p.map.setCenter(point);
 	 
	marker.addEventListener('dragend', function(e) {
 		var zoom= gtm.f.getZoom();
        var add_coord = "{'lat':'"+e.point.lat+"','lng':'"+e.point.lng+"','zoom':"+zoom+"}";
        var contentHtml = 'lat:'+e.point.lat+'<br/>lng:';
	  	  contentHtml+= e.point.lng+'<br/>';
	  	  gtm.p.lastInfoWindow.setContent(contentHtml);
	  	  gtm.p.coord = add_coord; 
 	  });
 	 
 	var contentHtml = '';
	 contentHtml+= 'lng:'+jsonObject.lng+'<br/>';
	 contentHtml+= 'lat:'+jsonObject.lat+'<br/>';
	 gtm.p.map.addOverlay(marker);
	 gtm.f.createInfoWindow(marker,contentHtml);
};

gtm.f.getZoom = function(){
  return gtm.p.map.getZoom();
};

gtm.f.setZoom = function(zoom){
	return gtm.p.map.setZoom(zoom);
};